package com.orion.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.orion.entity.User;
import com.orion.service.UserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @author Administrator
 * @date 2021/11/28
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource(name = "userService")
    private UserService userService;


    @GetMapping("/page")
    public IPage<User> page(Integer current ,Integer size, User user) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper
                .like(StringUtils.isNotEmpty(user.getAccount()), User::getAccount, user.getAccount())
                .like(StringUtils.isNotEmpty(user.getName()), User::getName, user.getName())
                .like(StringUtils.isNotEmpty(user.getEmail()), User::getEmail, user.getEmail())
                .like(StringUtils.isNotEmpty(user.getTel()), User::getTel, user.getTel())
                .like(Objects.nonNull(user.getId()), User::getId, user.getId())
                .ge(Objects.nonNull(user.getGeBirthday()), User::getBirthday, user.getGeBirthday())
                .le(Objects.nonNull(user.getLeBirthday()), User::getBirthday, user.getLeBirthday())
                .eq(Objects.nonNull(user.getStatus()), User::getStatus, user.getStatus());
        IPage<User> page = new Page<>(current, size);
        return userService.page(page, wrapper);
    }

    @GetMapping("/list")
    public List<User> list(User user) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper
                .like(StringUtils.isNotEmpty(user.getAccount()), User::getAccount, user.getAccount())
                .like(StringUtils.isNotEmpty(user.getName()), User::getName, user.getName())
                .like(StringUtils.isNotEmpty(user.getEmail()), User::getEmail, user.getEmail())
                .like(StringUtils.isNotEmpty(user.getTel()), User::getTel, user.getTel())
                .like(Objects.nonNull(user.getId()), User::getId, user.getId())
                .ge(Objects.nonNull(user.getGeBirthday()), User::getBirthday, user.getGeBirthday())
                .le(Objects.nonNull(user.getLeBirthday()), User::getBirthday, user.getLeBirthday())
                .eq(Objects.nonNull(user.getStatus()), User::getStatus, user.getStatus());
        return userService.list(wrapper);
    }

    @GetMapping("/save")
    public boolean save(User entity) {
        System.out.println(entity);
        return userService.save(entity);
    }

    @GetMapping("/saveOrUpdate")
    public boolean saveOrUpdate(User entity) {
        return userService.saveOrUpdate(entity);
    }

    @GetMapping("/update")
    public boolean update(User user) {
        LambdaUpdateWrapper<User> wrapper = new LambdaUpdateWrapper<>();
        wrapper
                .set(StringUtils.isNotEmpty(user.getAccount()), User::getAccount, user.getAccount())
                .set(StringUtils.isNotEmpty(user.getEmail()), User::getEmail, user.getEmail())
                .set(StringUtils.isNotEmpty(user.getTel()), User::getTel, user.getTel())
                .set(Objects.nonNull(user.getStatus()), User::getStatus, user.getStatus())
                .eq(User::getName,user.getName());
        return userService.update(null, wrapper);
    }

    @GetMapping("/updateById")
    public boolean updateById(User entity) {
        return userService.updateById(entity);
    }


    @GetMapping("/getById")
    public User getById(Integer id) {
        return userService.getById(id);
    }

    @GetMapping("/getOne")
    public User getOne(User user) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(User::getAccount, User::getId, User::getStatus);
        wrapper
                .like(StringUtils.isNotEmpty(user.getAccount()), User::getAccount, user.getAccount())
                .like(StringUtils.isNotEmpty(user.getName()), User::getName, user.getName())
                .like(StringUtils.isNotEmpty(user.getEmail()), User::getEmail, user.getEmail())
                .like(StringUtils.isNotEmpty(user.getTel()), User::getTel, user.getTel())
                .like(Objects.nonNull(user.getId()), User::getId, user.getId())
                .ge(Objects.nonNull(user.getGeBirthday()), User::getBirthday, user.getGeBirthday())
                .le(Objects.nonNull(user.getLeBirthday()), User::getBirthday, user.getLeBirthday())
                .eq(Objects.nonNull(user.getStatus()), User::getStatus, user.getStatus());

        return userService.getOne(wrapper);
    }
}
